package com.gitee.srurl.system.util.response;

import com.gitee.srurl.core.enums.ResponseCodeEnums;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author zxw
 * @date 2022/11/20
 */
@Data
@NoArgsConstructor
public class R<T> {

    private int code;

    private String msg;

    private T data;

    public static final String SUCCESS_MSG = "操作成功";
    public static final String ERROR_MSG = "操作失败";

    // --------------------------- ok -----------------------------

    public static <T> R<T> ok() {

        return buildResponse(ResponseCodeEnums.SUCCESS.code(), SUCCESS_MSG, null);
    }

    public static <T> R<T> ok(String msg) {

        return buildResponse(ResponseCodeEnums.SUCCESS.code(), msg, null);
    }

    public static <T> R<T> ok(T data) {

        return buildResponse(ResponseCodeEnums.SUCCESS.code(), SUCCESS_MSG, data);
    }

    /**
     * data 为 string 类型时使用
     * 如果用 {@link R#ok(String msg)} 会把 data 参数放在 msg 字段 不合适
     *
     * @param data data
     * @return R<String>
     */
    public static R<String> okStr(String data) {

        return buildResponse(ResponseCodeEnums.SUCCESS.code(), SUCCESS_MSG, data);
    }

    // --------------------------- error -----------------------------

    public static <T> R<T> error() {

        return buildResponse(ResponseCodeEnums.GLOBAL_ERROR.code(), ERROR_MSG, null);
    }

    public static <T> R<T> error(String msg) {

        return buildResponse(ResponseCodeEnums.GLOBAL_ERROR.code(), msg, null);
    }

    public static <T> R<T> error(T data) {

        return buildResponse(ResponseCodeEnums.GLOBAL_ERROR.code(), ERROR_MSG, data);
    }

    public static <T> R<T> error(String msg, T data) {

        return buildResponse(ResponseCodeEnums.GLOBAL_ERROR.code(), msg, data);
    }

    public static <T> R<T> error(int code) {

        return buildResponse(code, ERROR_MSG, null);
    }

    public static <T> R<T> error(int code, String msg) {

        return buildResponse(code, msg, null);
    }

    public static <T> R<T> error(ResponseCodeEnums responseCodeEnums) {

        return buildResponse(responseCodeEnums.code(), responseCodeEnums.message(), null);
    }

    public static <T> R<T> error(int code, T data) {

        return buildResponse(code, ERROR_MSG, data);
    }

    public static <T> R<T> error(int code, String msg, T data) {

        return buildResponse(code, msg, data);
    }

    // --------------------------- page -----------------------------
//    public static <T> R<PageResponse<T>> page(IPage<T> page) {
//
//        return buildPage(page);
//    }

    public static <T> R<PageResponse<T>> page(PageResponse<T> pageResponse) {

        return buildPage(pageResponse);
    }

    // --------------------------- 构造返回数据 -----------------------------

    private static <T> R<T> buildResponse(int code, String msg, T data) {

        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    // --------------------------- 构造分页返回数据 -----------------------------
//    private static <T> R<PageResponse<T>> buildPage(IPage<T> page) {
//
//        PageResponse<T> pageResponse = new PageResponse<>();
//        pageResponse.setRecords(page.getRecords());
//        pageResponse.setTotal(page.getTotal());
//        pageResponse.setCurrentPage(page.getCurrent());
//        pageResponse.setPageSize(page.getSize());
//
//        return buildPage(pageResponse);
//    }

    private static <T> R<PageResponse<T>> buildPage(PageResponse<T> pageResponse) {

        R<PageResponse<T>> r = new R<>();
        r.setCode(ResponseCodeEnums.SUCCESS.code());
        r.setMsg(SUCCESS_MSG);
        r.setData(pageResponse);
        return r;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }
}
